var ns;
app = new Vue({
    el: '#app',
    data: {
        showsearch: false,
        showsearchmonth: false,
        showsearchparam: false,
        showtime:false,
        ProjectName:'',
        ReportType:0,//0 设备巡检  1 安防巡更  2 环境保洁
        form: {
            startMonth: '', 
            activeDateDesc: '月份',
            ProjectCompanyType: 1,
            ProjectID:0,
            ProjectIDs: [],
        },
        summary: {},
        projectList: [], 
        WBlist:[],  ////维保数据
        XJlist:[],  ////巡检数据
        userList:[],  ////员工工时
        // WBlist:[{ "ID": 1, "name": "完成率", "y": 70,"DeviceClont":175,"TaskCount":2,"data":[{ "ID": 1, "name": "完成率", "y":70 },{ "ID": 2, "name": "未完成率", "y": 23 }]},{ "ID": 2, "name": "超时率", "y": 33,"DeviceClont":145,"TaskCount":54 ,"data":[{ "ID": 1, "name": "超时率", "y": 33 },{ "ID": 2, "name": "未超时率", "y": 52 }]}],
        // XJlist:[{ "ID": 1, "name": "完成率", "y": 63,"DeviceClont":54,"TaskCount":6,"data":[{ "ID": 1, "name": "完成率", "y":63 },{ "ID": 2, "name": "未完成率", "y": 35 }]},{ "ID": 2, "name": "超时率", "y": 23,"DeviceClont":3,"TaskCount":56 ,"data":[{ "ID": 1, "name": "超时率", "y": 23 },{ "ID": 2, "name": "未超时率", "y": 72 }]}],
        // userList:[
        //     {ID:1,UserName:'李涛',PhotoImg:'https://test.saasyy.com/upload/WechatServiceImg/20220218/752a16682505453cb0f6f8f00c926987_头像.jpg',DeviceCount:12,pjTime:2.34,allTime:2.3},
        //     {ID:2,UserName:'李文静',PhotoImg:'https://test.saasyy.com/upload/WechatServiceImg/20220218/752a16682505453cb0f6f8f00c926987_头像.jpg',DeviceCount:122,pjTime:2.34,allTime:2.3},
        // ],
    },
    methods: {
        open_task:function(){
            var that = this;
            var title='设备巡检任务列表';
            var url='equipment_tasklist';
            if(that.ReportType==1){
                title='安防巡更任务列表';
                url='patrolplan_tasklist';              
            }
            if(that.ReportType==2){
                title='环境保洁任务列表';
                url='bjlh_tasklist';    
            }  
            ns.openWin(url, title, {
                ProjectID: that.form.ProjectID,
                ProjectName:that.ProjectName,
                startMonth:that.form.startMonth,
            });
        },
        get_data: function () {
            var that = this;
            that.getSearchParams();
        },
        doSelectProject: function (ID,Name) {
            var that = this;
            that.ProjectName=Name;
            that.form.ProjectID=ID;
            that.form.ProjectIDs=[];
            that.form.ProjectIDs.push(ID);
            that.showsearchparam = false;
            that.doSearch();
        },
        //筛选数据
        getSearchParams: function () {
            var that = this;
            var options = {};
            options.action = "gettaskanalysisparams";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.projectList = data.projectList;
                    if(that.projectList!=undefined&&that.projectList.length>0){
                        that.doSelectProject(that.projectList[0].ID,that.projectList[0].Name); 
                    }
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        onShowTime:function(IsShow){
            var that=this;
            that.showtime=IsShow;
        },
        doSelectSearchParam: function (type) {
            var that = this;
            if (type == 1) { //筛选
                that.showsearchparam = !that.showsearchparam;
                that.showsearchmonth = false;
            }
            if (type == 2) { //按月
                that.showsearchparam = false;
                that.showsearchmonth = !that.showsearchmonth;
            }
            that.showsearch = that.showsearchparam || that.showsearchmonth;
            // if (type == 2 && that.showsearchmonth) {
            //     setTimeout(function () {
            //         that.initPicker();
            //     }, 100);
            // }
        },
        doHideSearch: function () {
            var that = this;
            that.showsearchparam = false;
            that.showsearchmonth = false;
            that.showsearch = false;
        },
        doSearch: function () {
            var that = this;
            that.doHideSearch();
            that.getAnalysisData();
        },
        getAnalysisData: function () {
            var that = this;
            //初始化
            that.XJlist=[];
            that.WBlist=[];
            that.userList=[];
            var planType = 1; ////planType 1 设备巡检  3 安防巡更  4 环境保洁
            if (that.ReportType == 1) {
                planType = 3;
            }
            if (that.ReportType == 2) {
                planType = 4;
            }

            var options = {};
            options.action = "gettaskanalysisdata"; ////方法名称
            options.projectIds = JSON.stringify(that.form.ProjectIDs);
            options.dateMonth = that.form.startMonth;
            // options.ProjectCompanyType = that.form.ProjectCompanyType; //筛选类型-项目
            options.planType = planType; 
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    if (that.ReportType == 0) {
                        that.summary=data.data1;
                        that.XJlist = that.dealData(data.data1);
                        that.WBlist = that.dealData(data.data2);
                    }
                    if (that.ReportType != 0) {
                        that.summary=data.data;
                        that.XJlist = that.dealData(data.data);
                    }
                    if (data.userList != undefined && data.userList.length > 0) {
                        that.userList = data.userList;
                    }
                    setTimeout(() => {
                        if (that.XJlist.length > 0) {
                            for (var i = 0; i < that.XJlist.length; i++) {
                                that.loadChart("containerxj" + i, i, that.XJlist[i]);
                            }
                        }
                        if (that.WBlist.length > 0) {
                            for (var i = 0; i < that.WBlist.length; i++) {
                                that.loadChart("containerwb" + i, i, that.WBlist[i]);
                            }
                        }
                    }, 300);
                   
                } 
                // else if (err) {
                //     api.toast({
                //         msg: err,
                //         duration: 2000,
                //         location: 'bottom'
                //     });
                // }
            }, {
                toast: true
            });
        },
        //拼接报表数据格式
        dealData:function(data){
            var that = this;
            var chartwc = [{ "name": "完成率", "y": data.completePercent1 }, { "name": "完成率", "y": data.completePercent2 }];
            var chartcs = [{ "name": "超时率", "y": data.timeoutPercent1 }, { "name": "超时率", "y": data.timeoutPercent2 }];
            var itemdata = [];
            if (that.ReportType == 0) {
                itemdata = [{ "name": "完成率", "y": data.completePercent1, "DeviceClont": data.totalDeviceCount, "TaskCount": data.totalCount, "data": chartwc },
                 { "name": "超时率", "y": data.timeoutPercent1,  "DeviceClont":  data.completeDeviceCount, "TaskCount":  data.completeCount, "data": chartcs }];
            }
            else {
                itemdata = [{ "name": "完成率", "y": data.completePercent1, "DeviceClont":0, "TaskCount": data.totalCount, "data": chartwc },
                { "name": "超时率", "y": data.timeoutPercent1,  "DeviceClont":  0, "TaskCount":  data.completeCount, "data": chartcs }];
            }
            return itemdata;
        },
        loadChart: function (IdName,index,item) {
            var processcolor=['#289fff', '#eee'];
            if(index%2>0){
                processcolor=['#ff0000', '#eee'];
            }
            var chart = new Highcharts.Chart({
                chart: {
                    renderTo: IdName,
                    spacing: [0, 0, 0, 0],
                    panning: false,
                },
                colors: processcolor,
                credits: {
                    enabled: false //不显示Highcharts
                },
                navigation: {
                    buttonOptions: {
                        enabled: false
                    }
                },
                title: {
                    floating: true,
                    text:item.y+'%',
                },
                tooltip: {
                     enabled:false,
                },
                plotOptions: {
                    pie: {
                        allowPointSelect: false,
                        cursor: 'pointer',
                        dataLabels: {
                            enabled: false,
                        },
                        // point: {
                        //     events: {
                        //         click: function(e) { // 同样的可以在点击事件里处理
                        //             chart.setTitle({
                        //                 text:e.point.y + ' %'
                        //             });
                        //         }
                        //     }
                        // },
                    }
                },
                series: [{
                    type: 'pie',
                    innerSize: '80%',
                    data: item.data
                }]
            }, function (c) { // 图表初始化完毕后的会掉函数
                // 环形图圆心
                var centerY = c.series[0].center[1],
                    titleHeight = parseInt(c.title.styles.fontSize);
                // 动态设置标题位置
                c.setTitle({
                    y: centerY + titleHeight / 2
                });
            });
        },
        initPicker: function () {
            var _this = this;
            (function ($, doc) {
                $.init();
                $.ready(function () {
                    //月份
                    var btn = document.querySelector('#datemonth');
                    btn.addEventListener('tap', function () {
                        var _self = this;
                        if (_self.picker) {
                            _self.picker.show(function (rs) {
                                _this.form.startMonth = rs.value;
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        } else {
                            var optionsJson = this.getAttribute('data-options') || '{}';
                            var options = JSON.parse(optionsJson);
                            var id = this.getAttribute('id');
                            _self.picker = new $.DtPicker(options);
                            _self.picker.show(function (rs) {
                                var timer = setTimeout(function () {
                                    clearTimeout(timer);
                                    _this.form.startMonth = rs.value;
                                    _this.doSearch();
                                }, 300);
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        }
                    }, false);
                });
            })(mui, document);
        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    var nowDate = new Date();
    var Y = nowDate.getFullYear();
    var M = nowDate.getMonth() + 1 < 10 ? '0' + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1;
    app.form.startMonth = Y+'-'+M;//初始化默认当前月
    app.ReportType=api.pageParam.servicetype || 0;
    app.initPicker();
    app.get_data();

};
